package org.example.firstproject.controller;

import org.example.firstproject.common.WrapMapper;
import org.example.firstproject.common.Wrapper;
import org.example.firstproject.dao.UserDao;
import org.example.firstproject.entity.User;
import org.example.firstproject.service.UserService;
import org.example.firstproject.vo.LoginVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//这是第一个接口

@RestController
@RequestMapping(value = "/user")
public class Firstcontroller {
    private static Map<String, User> userMap = new HashMap<>();
    static {
        User user = new User("user1", "123456", new String[]{"admin", "user"});
        User user1 = new User("user2", "123456789", new String[]{"user"});
        User user2 = new User("user3", "123456654321", new String[]{"user"});
        userMap.put(user.getUsername(), user);
        userMap.put(user1.getUsername(), user1);
        userMap.put(user2.getUsername(), user2);
    }
    @PostMapping("/login")
    public Wrapper login(@Validated @RequestBody LoginVo loginVo){
        String username = loginVo.getUsername();
        User user = userMap.get(username);
        if (user == null){
            return WrapMapper.error("用户名不存在");
        }else {
            if (loginVo.getPassword().equals(user.getPassword())){
                return WrapMapper.ok(user.getUsername());
            }else {
                return WrapMapper.error("密码错误");
            }
        }
    }
    @GetMapping("/getAllUser")
    public Wrapper getAllUser(){
        UserDao userService = null;
        List<User> userList = userService.getAllUser();
        return WrapMapper.ok(userList);
    }

    @GetMapping("/{userId}/getUser")
    public Wrapper getUser(@PathVariable String userId){
        UserService userService = null;
        User user = userService.getUserById(userId);
        if (user == null){
            return WrapMapper.error("用户不存在");
        }else {
            return WrapMapper.ok(user);
        }
    }

    @PostMapping("/zc")
    public Wrapper zc(@Validated @RequestBody User user){
        String username = user.getUsername();
        if (userMap.containsKey(username)){
            return WrapMapper.error("用户名重复");
        }else {
            userMap.put(username,user);
            return WrapMapper.ok("注册成功");
        }
    }

    @PostMapping("/password")
    public Wrapper password(@Validated @RequestBody LoginVo loginVo){
        String username = loginVo.getUsername();
        User user = userMap.get(username);
        if (user == null){
            return WrapMapper.error("没有此用户");
        }else {
            if (loginVo.getPassword().equals(user.getPassword())){
                user.setPassword(loginVo.getPasswordNew());
                userMap.put(username,user);
                return WrapMapper.ok();
            }else {
                return WrapMapper.error("旧密码错误");
            }
        }
    }

    @PostMapping("/delete")
    public Wrapper delete(@RequestParam String username){
        if (userMap.containsKey(username)){
            userMap.remove(username);
        }
        return WrapMapper.ok();
    }
}
